<template>
  <div class="login">
    <el-tabs v-model="activeName">
      <el-tab-pane label="登录" name="first">
        <div class="fromBox">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账号" prop="username">
              <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="success" round @click="submitForm('ruleForm')"
                >登录</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-tab-pane>
      <el-tab-pane label="注册" name="second">
        <div class="fromBox">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="signForm"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="账号" prop="username">
              <el-input v-model="signForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input  v-model="signForm.password"></el-input>
            </el-form-item>
            <el-form-item label="昵称" prop="password">
              <el-input v-model="signForm.nickname"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="parmas" round @click="submitSignForm()"
                >注册账号</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
// import axios from "../utils/request"
import { mapActions } from "vuex";
export default {
  data() {
    return {
      activeName: "first",
      ruleForm: {
        username: "",
        password: "",
      },
      signForm: {
        username: "",
        password: "",
        nickname:''
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    ...mapActions({
      getLogin: "login/getLogin",
      getSign:"signIn/getSign"
    }),
    submitForm(formName) {
      // console.log(this.ruleForm)
      this.getLogin(this.ruleForm)
        .then((res) => {
          this.$message({ message: res, type: "success" });
          this.$router.push('/home')
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    submitSignForm(){
      // console.log(this.signForm)
      this.getSign(this.signForm)
        .then((res)=>{
          this.$message({ message: res, type: "success" });
          this.activeName="first"
        })
        .catch((err)=>{
          this.$message.error(err);
        })
    }
  },
};
</script>
<style lang="scss">
.login {
  width: 500px;
  height: 400px;
  background: #fff;
  border-radius: 15px;
  padding: 20px;
  margin: 100px auto;
  .fromBox {
    width: 300px;
    height: auto;
    margin: 0 auto;
  }
}
</style>
